package com.ruoyi.web.controller.members;

import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.common.constant.UnionConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.UnionsStatus;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.members.domain.SysUnionMembers;
import com.ruoyi.members.service.ISysUnionMembersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 基层工会会员Controller
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/sys/unionsMembers")
public class SysUnionMembersController extends BaseController
{
    @Autowired
    private ISysUnionMembersService sysUnionMembersService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 组织工作 - 现任领导 - 询基列表
     */
//    @PreAuthorize("@ss.hasPermi('system:sysUnionMembers:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUnionMembers sysUnionMembers)
    {
        startPage();
        List<SysUnionMembers> list = sysUnionMembersService.selectSysUnionMembersList(sysUnionMembers);
        return getDataTable(list);
    }

    /**
     * 组织工作 - 现任领导 - 导出列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysUnionMembers:export')")
    @Log(title = "基层工会会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUnionMembers sysUnionMembers)
    {
        List<SysUnionMembers> list = sysUnionMembersService.selectSysUnionMembersList(sysUnionMembers);
        ExcelUtil<SysUnionMembers> util = new ExcelUtil<SysUnionMembers>(SysUnionMembers.class);
        util.exportExcel(response, list, "基层工会现任领导数据","基层工会现任领导信息表");
    }

    /**
     * 组织工作 - 现任领导 - 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysUnionMembers:query')")
    @GetMapping(value = "/{membersId}")
    public AjaxResult getInfo(@PathVariable("membersId") Long membersId)
    {
        return success(sysUnionMembersService.getById(membersId));
    }

    /**
     * 组织工作 - 现任领导 - 新增
     */
    @PreAuthorize("@ss.hasPermi('system:sysUnionMembers:add')")
    @Log(title = "基层工会会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUnionMembers sysUnionMembers)
    {
        sysUnionMembers.setCreateBy(SecurityUtils.getUsername());
        UnionsStatus unionsStatus = sysUnionMembersService.saveUnionsMembers(sysUnionMembers);
        return UnionConstants.SUCCESS_CODE.equals(unionsStatus.getCode())?success(unionsStatus.getInfo()):error(unionsStatus.getInfo());
    }

    /**
     * 组织工作 - 现任领导 - 修改
     */
    @PreAuthorize("@ss.hasPermi('system:sysUnionMembers:edit')")
    @Log(title = "基层工会会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUnionMembers sysUnionMembers)
    {
        //根据ID查询会员
        SysUnionMembers member = sysUnionMembersService.getById(sysUnionMembers.getMembersId());
        //判断会员是否存在（ID）
        if (member == null){
            return error(UnionsStatus.UNIONS_MEMBERS_NOT_EXIST.getInfo());
        }
        //是否修改标志
        boolean flag = false;
        //修改工号
        if (sysUnionMembers.getMembersNum() != member.getMembersNum()){
            flag = true;
        }
        //修改昵称
        if (sysUnionMembers.getMembersName() != member.getMembersName()){
            flag = true;
        }

        //根据工会查询用户
        SysUser user = sysUserService.getById(member.getUserId());
        //判断用户是否存在
        if (user == null){
            return error(UnionsStatus.USER_NOT_EXIST.getInfo());
        }

        //修改用户手机号码
        user.setPhonenumber(sysUnionMembers.getMembersTel());
        //修改用户性别
        user.setSex(sysUnionMembers.getMembersSex());
        //判断是否修改工号、昵称
        if (flag){
            //修改用户名
            user.setUserName(sysUnionMembers.getMembersNum().toString());
            //修改用户昵称
            user.setNickName(sysUnionMembers.getMembersName());
        }
        //更新用户表
        sysUserService.updateById(user);
        //更新会员表
        return toAjax(sysUnionMembersService.updateById(sysUnionMembers));
    }

    /**
     * 组织工作 - 现任领导 - 删除
     */
    @PreAuthorize("@ss.hasPermi('system:sysUnionMembers:remove')")
    @Log(title = "基层工会会员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{membersIds}")
    public AjaxResult remove(@PathVariable Long[] membersIds)
    {
        UnionsStatus unionsStatus = sysUnionMembersService.deleteUnionsMembersByMemsIds(membersIds);
        return UnionConstants.SUCCESS_CODE.equals(unionsStatus.getCode())?success(unionsStatus.getInfo()):error(unionsStatus.getInfo());
    }
}
